//
//  RAPNewFeatureViewController.swift
//  COPYCAT-Weibo
//
//  Created by 闫萌 on 12/30/15.
//  Copyright © 2015 RendezvousAuParadis. All rights reserved.
//

import UIKit

class RAPNewFeatureViewController: UIViewController, UIScrollViewDelegate {

    // MARK: - 懒加载控件
    lazy var scrollView: UIScrollView = {
        let scrollView = UIScrollView(frame: self.view.bounds)
        scrollView.delegate = self
        
        let imgW = kScreenBounds.width
        
        // 添加imageView
        let count = 4
        for i in 1...count {
            let img = UIImage(named: "new_feature_\(i)")
            let imgView = UIImageView(image: img)
            imgView.frame = CGRect(x: imgW * CGFloat(i - 1), y: 0, width: imgW, height: kScreenBounds.height)
            
            if i == 4 {
                imgView.userInteractionEnabled = true
                self.setupButtonsOfLastImageView(imgView)
            }
            
            scrollView.addSubview(imgView)
        }
        // 设置contentSize
        scrollView.contentSize = CGSize(width: CGFloat(count) * imgW, height: kScreenBounds.height)

        // 其他设置
        scrollView.bounces = false
        scrollView.showsHorizontalScrollIndicator = false
        scrollView.pagingEnabled = true
        
        return scrollView
        }()
    
    private func setupButtonsOfLastImageView(last: UIImageView) {
        // 分享按钮
        let shareButton = UIButton(title: "分享给小伙伴", fontSize: 14, imageName: "new_feature_share_false", backgroundImageName: nil, target: self, action: "sharedButtonClicked:", textColor: UIColor.darkGrayColor())
        shareButton.setImage(UIImage(named: "new_feature_share_true"), forState: UIControlState.Selected)
        shareButton.imageEdgeInsets = UIEdgeInsets(top: 0, left: -2, bottom: 0, right: 2)
        shareButton.titleEdgeInsets = UIEdgeInsets(top: 0, left: 2, bottom: 0, right: -2)
        
        shareButton.center.x = CGRectGetMidX(last.bounds)
        shareButton.center.y = kScreenBounds.height * 0.65
        
        // 进入微博
        let enterButton = UIButton(title: "进入微博", fontSize: 16, imageName: nil, backgroundImageName: "new_feature_finish_button",  target: self, action: "enterButtonClicked:", textColor: UIColor.whiteColor())
        enterButton.center.x = CGRectGetMidX(last.bounds)
        enterButton.center.y = kScreenBounds.height * 0.7
        
        last.addSubview(shareButton)
        last.addSubview(enterButton)
    }
    
    @objc private func sharedButtonClicked(sender: UIButton) {
        sender.selected = !sender.selected
    }
    
    @objc private func enterButtonClicked(sender: UIButton) {
        RAPLog("进入微博")
        // 正式进入此版本，将版本号写入userdefalt，以便日后比较
        // 将info,plist中的版本号写入userdefault
        NSUserDefaults.standardUserDefaults().setObject(NSBundle.mainBundle().infoDictionary!["CFBundleVersion"], forKey: "RAPCurrentVersion")
        
        // 跳转至RAPMainInterfaceTabBar
        NSNotificationCenter.defaultCenter().postNotificationName(RAPEnterMainInterfaceNotification, object: nil)
    }
    
    lazy var pageControl: UIPageControl = {
        
        let pageControl = UIPageControl()
        
        // 添加约束
        pageControl.center.x = CGRectGetMidX(self.view.bounds)
        pageControl.center.y = kScreenBounds.height * 0.9
        pageControl.currentPageIndicatorTintColor = UIColor.orangeColor()
        pageControl.pageIndicatorTintColor = UIColor.lightGrayColor()
        pageControl.numberOfPages = 4
        
        return pageControl
        }()

    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        setupUI()
    }
    
    private func setupUI() {
        // scroll view
        view.addSubview(scrollView)
        view.addSubview(pageControl)
    }
}

// MARK: - <UIScrollViewDelegate>
extension RAPNewFeatureViewController {
    func scrollViewDidScroll(scrollView: UIScrollView) {
        pageControl.currentPage = Int(scrollView.contentOffset.x / kScreenBounds.width + 0.5)
    }
}
